package com.zqxx.examine.portal.security;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import com.zqxx.examine.model.user.User;
import com.zqxx.examine.portal.persistence.UserMapper;

@Repository("userDetailsServiceImpl2")
public class UserDetailsServiceImpl2
  implements UserDetailsService
{
  private UserInfo userInfo;
  @Autowired
  public UserMapper userMapper;
  
  public UserDetails loadUserByUsername(String username)
    throws UsernameNotFoundException
  {
    User user = this.userMapper.getUserByName(username);
    if (user == null) {
      throw new UsernameNotFoundException("user not found!");
    }
    List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles());
    this.userInfo = new UserInfo(username, "", user.isEnabled(), true, true, true, authorities);
    this.userInfo.setUserid(user.getUserId());
    this.userInfo.setRolesName(user.getRoles());
    this.userInfo.setTrueName(user.getTrueName());
    this.userInfo.setEmail(user.getEmail());
    this.userInfo.setPhoneNum(user.getPhoneNum());
    this.userInfo.setNationalId(user.getNationalId());
    this.userInfo.setDepId(user.getDepId());
    return this.userInfo;
  }
}

